package com.cskaoyan.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.mapper.CommentMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;

import java.util.List;

/**
 * @author xudengfeng
 * @date 2020/12/6 15:47
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    CommentMapper commentMapper;

    @Override
    public ListData<Comment> queryComment(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        PageHelper.startPage(page, limit);
        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(sort + " " + order);
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }

        List<Comment> comments = commentMapper.selectByExample(commentExample);

        ListData<Comment> commentListData = new ListData<>();
        commentListData.setItems(comments);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();
        commentListData.setTotal((int) total);

        return commentListData;
    }

    @Override
    public int deleteCommentById(Integer id) {
        return commentMapper.deleteByPrimaryKey(id);
    }
}
